﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SharpKit.JavaScript;
using SharpKit.Html4;
using SharpKit.jQuery;

namespace WebDesigner.Templates
{
    [JsType(JsMode.Prototype)]
    public partial class PropertyGrid : jQueryContext
    {
        public PropertyGrid(JsString selector)
        {
            Selector = J(selector);
            Selector.data("Control", this.As<JsObject>());//new { Control = this.As<JsObject>() }.As<JsObject>());
            //Current = this;
        }
        //static PropertyGrid Current;

        jQuery Selector;
        static void PropertyGrid_click(HtmlDomEventArgs e)
        {
            var row = J(e.srcElement).parents("TR").first().get(0).As<HtmlTableRow>();
            var pg = J(e.srcElement).parents(".PropertyGrid").first().data("Control").As<PropertyGrid>();
            pg.EditRow(row);
            //Current.EditRow(row);
        }
        public HtmlTableRow CurrentRow { get; private set; }

        void EditRow(HtmlTableRow row)
        {
            if (CurrentRow == row)
                return;
            if (CurrentRow != null)
                OnFinishEdit(CurrentRow);
            if (row == null)
                return;
            var item = GetItem(row);
            var rowEdit = ReplaceRow(row, "#PropertyGridRowEditTemplate");
            CurrentRow = rowEdit;
            if(BeginEdit!=null)
                BeginEdit(item);
        }

        public PropertyGridItem GetItem(HtmlTableRow row)
        {
            if (row == null)
                return null;
            var row2 = J(row);
            var item = new PropertyGridItem();
            item.Value = row2.find(".CellValue").text();
            item.Name = row2.find(".CellName").text();
            return item;
        }
        HtmlTableRow ReplaceRow(HtmlTableRow row, JsString template=null)
        {
            var item = GetItem(row);
            var newRow = CreateRow(item, template);
            J(row).after(newRow);
            J(row).remove();
            return newRow;
        }
        public JsAction<PropertyGridItem> FinishEdit { get; set; }
        public JsAction<PropertyGridItem> BeginEdit { get; set; }
        private void OnFinishEdit(HtmlTableRow row)
        {
            var row2 = ReplaceRow(row);
            var item = GetItem(row2);
            if (FinishEdit != null)
                FinishEdit(item);
        }

        private HtmlTableRow CreateRow(PropertyGridItem item, JsString template = null)
        {
            var row2 = J(template ?? "#PropertyGridRowTemplate").clone();
            var row = row2.get(0).As<HtmlTableRow>();
            row2.attr("id", "");
            SetItem(row, item);
            return row;
        }

        public void SetItem(HtmlTableRow row, PropertyGridItem item)
        {
            var row2 = J(row);
            row2.find(".CellName").text(item.Name).val(item.Name);
            row2.find(".CellValue").text(item.Value).val(item.Value);
        }
        public HtmlTableRow AddRow(PropertyGridItem item)
        {
            var row = CreateRow(item);
            J(".PropertyGrid").append(row);
            return row;
        }

        public void DeleteRows()
        {
            J(".PropertyGrid TBODY").first().children().remove();
        }


        public PropertyGridItem GetCurrentItem()
        {
            return GetItem(CurrentRow);
        }
    }

    [JsType(JsMode.Json)]
    public class PropertyGridItem
    {
        public JsString Name { get; set; }
        public JsString Value { get; set; }
    }

}